<!--

    Copyright © 2016-2021 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section style="padding-bottom: 16px; margin: 0">
  <mat-tab-group dynamicHeight>
    <mat-tab label="{{ 'device-profile.lwm2m.model-tab' | translate }}">
      <ng-template matTabContent>
        <section [formGroup]="lwm2mDeviceProfileFormGroup">
          <tb-profile-lwm2m-object-list
            (addList)="addObjectsList($event)"
            (removeList)="removeObjectsList($event)"
            formControlName="objectIds">
          </tb-profile-lwm2m-object-list>
          <tb-profile-lwm2m-observe-attr-telemetry
            formControlName="observeAttrTelemetry">
          </tb-profile-lwm2m-observe-attr-telemetry>
        </section>
      </ng-template>
    </mat-tab>
    <mat-tab label="{{ 'device-profile.lwm2m.servers' | translate }}">
      <section [formGroup]="lwm2mDeviceProfileFormGroup">
        <section formGroupName="bootstrap" style="padding: 4px 2px">
          <mat-accordion multi="true">
            <mat-expansion-panel>
              <mat-expansion-panel-header>
                <mat-panel-title>{{ 'device-profile.lwm2m.servers' | translate }}</mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent formGroupName="servers">
                <div fxLayout="row" fxLayout.xs="column" fxLayoutGap="8px" fxLayoutGap.xs="0px">
                  <mat-form-field fxFlex>
                    <mat-label>{{ 'device-profile.lwm2m.short-id' | translate }}</mat-label>
                    <input matInput type="number" min="1" max="65534" formControlName="shortId" required>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.shortId').hasError('required')">
                      {{ 'device-profile.lwm2m.short-id-required' | translate }}
                    </mat-error>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.shortId').hasError('min') ||
                                        lwm2mDeviceProfileFormGroup.get('bootstrap.servers.shortId').hasError('max')">
                      {{ 'device-profile.lwm2m.short-id-range' | translate }}
                    </mat-error>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.shortId').hasError('pattern')">
                      {{ 'device-profile.lwm2m.short-id-pattern' | translate }}
                    </mat-error>
                  </mat-form-field>
                  <mat-form-field fxFlex>
                    <mat-label>{{ 'device-profile.lwm2m.lifetime' | translate }}</mat-label>
                    <input matInput type="number" min="0" formControlName="lifetime" required>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.lifetime').hasError('required')">
                      {{ 'device-profile.lwm2m.lifetime-required' | translate }}
                    </mat-error>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.lifetime').hasError('pattern') ||
                                        lwm2mDeviceProfileFormGroup.get('bootstrap.servers.lifetime').hasError('min')">
                      {{ 'device-profile.lwm2m.lifetime-pattern' | translate }}
                    </mat-error>
                  </mat-form-field>
                  <mat-form-field fxFlex>
                    <mat-label>{{ 'device-profile.lwm2m.default-min-period' | translate }}</mat-label>
                    <input matInput type="number" min="0" formControlName="defaultMinPeriod" required>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.defaultMinPeriod').hasError('required')">
                      {{ 'device-profile.lwm2m.default-min-period-required' | translate }}
                    </mat-error>
                    <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('bootstrap.servers.defaultMinPeriod').hasError('pattern') ||
                                        lwm2mDeviceProfileFormGroup.get('bootstrap.servers.defaultMinPeriod').hasError('min')">
                      {{ 'device-profile.lwm2m.default-min-period-pattern' | translate }}
                    </mat-error>
                  </mat-form-field>
                </div>
                <mat-form-field class="mat-block">
                  <mat-label>{{ 'device-profile.lwm2m.binding' | translate }}</mat-label>
                  <mat-select formControlName="binding">
                    <mat-option *ngFor="let bindingMode of bindingModeTypes" [value]="bindingMode">
                      {{ bindingModeTypeNamesMap.get(bindingMode) | translate }}
                    </mat-option>
                  </mat-select>
                </mat-form-field>
                <mat-checkbox formControlName="notifIfDisabled" color="primary">
                  {{ 'device-profile.lwm2m.notification-storing' | translate }}
                </mat-checkbox>
              </ng-template>
            </mat-expansion-panel>
            <mat-expansion-panel>
              <mat-expansion-panel-header>
                <mat-panel-title>{{ 'device-profile.lwm2m.bootstrap-server' | translate }}</mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                <tb-profile-lwm2m-device-config-server
                  formControlName="bootstrapServer"
                  [isBootstrapServer]="true">
                </tb-profile-lwm2m-device-config-server>
              </ng-template>
            </mat-expansion-panel>
            <mat-expansion-panel>
              <mat-expansion-panel-header>
                <mat-panel-title>{{ 'device-profile.lwm2m.lwm2m-server' | translate }}</mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                <tb-profile-lwm2m-device-config-server
                  formControlName="lwm2mServer"
                  [isBootstrapServer]="false">
                </tb-profile-lwm2m-device-config-server>
              </ng-template>
            </mat-expansion-panel>
          </mat-accordion>
        </section>
      </section>
    </mat-tab>
    <mat-tab label="{{ 'device-profile.lwm2m.others-tab' | translate }}">
      <ng-template matTabContent [formGroup]="lwm2mDeviceProfileFormGroup">
        <section formGroupName="clientLwM2mSettings">
          <fieldset class="fields-group">
            <legend class="group-title" translate>device-profile.lwm2m.fw-update</legend>
            <mat-form-field class="mat-block" fxFlex>
              <mat-label>{{ 'device-profile.lwm2m.fw-update-strategy' | translate }}</mat-label>
              <mat-select formControlName="fwUpdateStrategy">
                <mat-option [value]=1>{{ 'device-profile.lwm2m.fw-update-strategy-package' | translate }}</mat-option>
                <mat-option [value]=2>{{ 'device-profile.lwm2m.fw-update-strategy-package-uri' | translate }}</mat-option>
                <mat-option [value]=3>{{ 'device-profile.lwm2m.fw-update-strategy-data' | translate }}</mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="mat-block" fxFlex *ngIf="lwm2mDeviceProfileFormGroup.get('clientLwM2mSettings.fwUpdateStrategy').value === 2">
              <mat-label>{{ 'device-profile.lwm2m.fw-update-resource' | translate }}</mat-label>
              <input matInput formControlName="fwUpdateResource" required>
              <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('clientLwM2mSettings.fwUpdateResource').hasError('required')">
                {{ 'device-profile.lwm2m.fw-update-resource-required' | translate }}
              </mat-error>
            </mat-form-field>
          </fieldset>
          <fieldset class="fields-group">
            <legend class="group-title" translate>device-profile.lwm2m.sw-update</legend>
            <mat-form-field class="mat-block" fxFlex>
              <mat-label>{{ 'device-profile.lwm2m.sw-update-strategy' | translate }}</mat-label>
              <mat-select formControlName="swUpdateStrategy">
                <mat-option [value]=1>{{ 'device-profile.lwm2m.sw-update-strategy-package' | translate }}</mat-option>
                <mat-option [value]=2>{{ 'device-profile.lwm2m.sw-update-strategy-package-uri' | translate }}</mat-option>
              </mat-select>
            </mat-form-field>
            <mat-form-field class="mat-block" fxFlex *ngIf="lwm2mDeviceProfileFormGroup.get('clientLwM2mSettings.swUpdateStrategy').value === 2">
              <mat-label>{{ 'device-profile.lwm2m.sw-update-resource' | translate }}</mat-label>
              <input matInput formControlName="swUpdateResource" required>
              <mat-error *ngIf="lwm2mDeviceProfileFormGroup.get('clientLwM2mSettings.swUpdateResource').hasError('required')">
                {{ 'device-profile.lwm2m.sw-update-resource-required' | translate }}
              </mat-error>
            </mat-form-field>
          </fieldset>
          <fieldset class="fields-group">
            <legend class="group-title" translate>device-profile.power-saving-mode</legend>
            <tb-power-mode-settings [parentForm]="clientSettingsFormGroup">
            </tb-power-mode-settings>
          </fieldset>
          <mat-slide-toggle class="mat-slider"
           formControlName="compositeOperationsSupport">{{ 'device-profile.lwm2m.composite-operations-support' | translate }}</mat-slide-toggle>
<!--          <mat-accordion multi="true">-->
<!--            <div *ngIf="false">-->
<!--              <mat-expansion-panel>-->
<!--                <mat-expansion-panel-header>-->
<!--                  <mat-panel-title>{{ 'device-profile.lwm2m.client-strategy' | translate }}</mat-panel-title>-->
<!--                </mat-expansion-panel-header>-->
<!--                <ng-template matExpansionPanelContent>-->
<!--                  <div fxLayout="column">-->
<!--                    <mat-form-field class="mat-block">-->
<!--                      <mat-label>{{ 'device-profile.lwm2m.client-strategy-label' | translate }}</mat-label>-->
<!--                      <mat-select formControlName="clientOnlyObserveAfterConnect">-->
<!--                        <mat-option value=1>{{ 'device-profile.lwm2m.client-strategy-only-observe' | translate }}</mat-option>-->
<!--                        <mat-option value=2>{{ 'device-profile.lwm2m.client-strategy-read-all' | translate }}</mat-option>-->
<!--                      </mat-select>-->
<!--                    </mat-form-field>-->
<!--                  </div>-->
<!--                </ng-template>-->
<!--              </mat-expansion-panel>-->
<!--            </div>-->
<!--          </mat-accordion>-->
        </section>
      </ng-template>
    </mat-tab>
    <mat-tab label="{{ 'device-profile.lwm2m.config-json-tab' | translate }}">
      <ng-template matTabContent>
        <section style="padding: 8px 0">
          <tb-json-object-edit
            readonly
            [sort]="sortFunction"
            label="{{ 'device-profile.transport-type-lwm2m' | translate }}"
            [ngModel]="configurationValue">
          </tb-json-object-edit>
        </section>
      </ng-template>
    </mat-tab>
  </mat-tab-group>
</section>
